
// 响应信息res.json
const responseInfo = require('../utils/responseInfo')
// fs
const { readFileSync, writeFileSync } = require('fs')
// path
const path = require('path')

const userPath = path.join(__dirname, '../', 'database', 'data.json')

module.exports = {
   
    // 登录
    login(req, res) {
        let { username, password } = req.body
        if (!username || !password) {
            responseInfo(res, 501, '账号或密码为空')
            return
        }
        if (username === 'admin' && password === '123456') {
            responseInfo(res, 200, '登陆中~')
            return
        }
    },
    // 添加用户
    add(req, res) {
        let { username, password, email, sex, tel } = req.body
        if (!username || !password || !email || !sex || !tel) {
            responseInfo(res, 501, '数据不可为空')
            return
        }
        // 读数据
        let data = readFileSync(userPath, 'utf-8');
        // 没有数据就创建
        if (data === "") {
            data = '[]'
        }
        data = JSON.parse(data);
        // 有数据就判断账号是否存在
        let flag = true;
        data.forEach(items => {
            if (items.username === username) {
                responseInfo(res, 402, '用户已存在')
                flag = false
                return
            }
        })
        // 走到这一步就可以添加账号密码了
        if (flag) {
            data.push({
                username,
                password,
                email,
                sex,
                tel
            });
            writeFileSync(userPath, JSON.stringify(data));
            responseInfo(res, 200, '添加成功')
        }
    },
    // 获取用户信息
    getUser(req, res) {
        // 读数据
        let data = readFileSync(userPath, 'utf-8');
        // 没有数据就创建
        if (data === "") {
            data = '[]'
        }
        // 不管是否读到数据data中都会有json对象的字符串，所以放在这里转成json对象
        data = JSON.parse(data);
        responseInfo(res, 200, '用户信息获取成功', {
            userInfo: data
        })
    },
    // 修改
    update(req, res) {
        let { beforeusername, username, password, email, sex, tel } = req.body
        if (!beforeusername || !username || !password || !email || !sex || !tel) {
            responseInfo(res, 501, '数据不可为空')
            return
        }
        // 读数据
        let data = readFileSync(userPath, 'utf-8');
        data = JSON.parse(data);
        data.forEach(items => {
            if (items.username === beforeusername) {
                items.username = username
                items.password = password
                items.email = email
                items.sex = sex
                items.tel = tel
                return
            }
        })
        writeFileSync(userPath, JSON.stringify(data));
        responseInfo(res, 200, '用户信息修改成功')
    },
    // 删除
    del(req, res) {
        let { username } = req.body
        // 读数据
        let data = readFileSync(userPath, 'utf-8');
        data = JSON.parse(data);
        data.forEach((items, index) => {
            if (items.username === username) {
                data.splice(index, 1)
                return
            }
        })
        writeFileSync(userPath, JSON.stringify(data));
        responseInfo(res, 200, '该用户已删除')
    }
}